<template>
  <div id="app">
    <el-row type="flex" align="middle" style="height: 650px">
      <el-col :offset="8">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>科泰登录</span>
          </div>
          <el-form :model="form" ref="form" :rules="rules" label-width="80px">
            <el-form-item label="用户名" prop="account">
              <el-input v-model="form.account"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('form')"
                >登录</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import qs from "qs";
export default {
  data() {
    return {
      form: {
        account: "",
        password: "",
      },

      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 6, message: "长度在 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(f1) {
      this.$refs[f1].validate((valid) => {
        console.log(valid);
        if (valid) {
          console.log("submit");
          //let queryStr = qs.stringify(this.form);
          //  console.log(queryStr);
          this.$axios
            .post("http://localhost:8080/ch15/user/doLogin", this.form, {
              headers: {
                token: "123",
              },
            })
            .then((res) => {
              console.log(res);
              if (res.data.code === 0) {
                let userInfo = res.data.data;
                this.$message.success(`${userInfo.account}登录成功!`);
                //  通过 commit  调用 mutations  中的 login  函数，将用户信息保 存到 到 Vuex
                this.$store.commit("login", userInfo);
                this.$router.push("/main");
              } else {
                this.$message.error(res.data.msg);
              }
            });
        } else {
          this.$message.success(`验证失败，请重新填写`);
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 400px;
}
</style>
